package com.yuang.library.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.yuang.library.R;

public class SimpleDialog extends Dialog {

    protected Context mContext;

    protected LayoutParams mLayoutParams;
    private View dialog_view;

    public LayoutParams getLayoutParams() {
        return mLayoutParams;
    }

    public SimpleDialog(Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    public SimpleDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView(context);
    }

    public SimpleDialog(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setBackgroundDrawableResource(R.drawable.transparent_bg);
        dialog_view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_simple, null);
        mContext = context;
        Window window = this.getWindow();
        mLayoutParams = window.getAttributes();
        mLayoutParams.alpha = 1f;
        window.setAttributes(mLayoutParams);
        if (mLayoutParams != null) {
            mLayoutParams.height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
            mLayoutParams.gravity = Gravity.CENTER;
        }

        dialog_view.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel();
            }
        });


        setContentView(dialog_view);
    }

    public void hideClose() {
        dialog_view.findViewById(R.id.close).setVisibility(View.GONE);
    }

    public void setListener(View.OnClickListener l) {
        dialog_view.findViewById(R.id.ok).setOnClickListener(l);
    }

    public void setMessage(String message) {
        ((TextView) dialog_view.findViewById(R.id.msg)).setText(message);
    }
}
